package main

import (
	"log"
	"os"
	"os/signal"
	"server/src/logging"
	"server/src/storage"
	"server/src/zyplog"
	"syscall"
	"time"
)

// 版本号
const VER string = "胎温胎压数据处理服务器 v2.0 build 240202"

func main() {
	initGlobal()

	storage.Init(storage.RootPath())

	// 启动日志
	logging.DisableBootScreenLog()
	logging.LogBoot()

	zLog(VER, " 启动")

	cfg, err := loadConfig("")
	if err != nil {
		log.Println(VER, err.Error())
		return
	}

	var logType zyplog.LogType
	switch cfg.Log.Type {
	case 0:
		logType = zyplog.LOG_STDOUT
	case 1:
		logType = zyplog.LOG_FILE
	case 2:
		logType = zyplog.LOG_SYSLOG
	}
	g_log, err = zyplog.NewZypLoger(logType, cfg.Log.Path)
	if err != nil {
		log.Println("创建日志失败：", err.Error())
		return
	}
	defer func() {
		zLog("日志关闭")
		g_log.Close()
	}()

	dbo, err := NewDBOperation(cfg)
	if err != nil {
		zLogError("数据库连接失败，程序退出:", err)
		return
	} else {
		zLog("数据库连接成功")
	}
	defer dbo.Close()

	// 线程数量
	processor := NewProcessor(dbo, cfg)
	for i := 0; i < cfg.ProcessorNum; i++ {
		go processor.Run()
		time.Sleep(2 * time.Second)
	}

	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

	s := <-signalChan
	zLog("服务器收到退出信号:", s)
}
